package com.martin.fastframe.ui.flow

import androidx.lifecycle.viewModelScope
import com.martin.fastframe.ui.main.TextViewHolder
import com.martin.lib_base.base.BaseViewModel
import com.martin.lib_base.constant.ConstantKey
import com.martin.lib_base.impl.FlowBus
import com.martin.lib_base.impl.LiveDataBus
import com.martin.lib_base.ktx.loge
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

class FlowViewModel : BaseViewModel() {

    val viewHolders = listOf(TextViewHolder::class.java)

    val items = listOf(
        "测试切换线程",
        "SharedFlow",
        "FlowBus Demo",
        "Demo Init",
        "Job cancel"
    )

    private var flowModel: FlowModel? = null

    fun onItemClick(position: Int) {
        when (items[position]) {
            //测试切换线程
            "测试切换线程" -> {
                testSwitchThread()
            }

            "SharedFlow" -> {
                testSharedFlow()
            }

            "FlowBus Demo" -> {
                FlowBus.with<String>(ConstantKey.VIDEO).tryEmit("视频消息")
                LiveDataBus.with(ConstantKey.VIDEO).postValue("视频消息")
            }

            "Demo Init" -> {
                flowModel?.release()
                flowModel = FlowModel()
                flowModel?.init()
            }

            "Job cancel" -> {
                flowModel?.release()
            }
        }
    }

    private fun testSharedFlow() {
        val sharedFlow = MutableSharedFlow<String>(1, 1)
        viewModelScope.launch(Dispatchers.IO) {
            sharedFlow.emit("emit 当前的线程 : ${Thread.currentThread().name}")
        }

        viewModelScope.launch {
            sharedFlow.collect {
                loge(it)
                loge("collect 当前的线程 : ${Thread.currentThread().name}")
            }
        }

        sharedFlow.onEach {
            loge(it)
            loge("onEach 当前的线程 : ${Thread.currentThread().name}")
        }.launchIn(viewModelScope)

    }

    private fun testSwitchThread() {
        val f = flow {
            emit(0)
            loge("emit 当前的线程是：${Thread.currentThread().name}")
        }.flowOn(Dispatchers.IO)
            .map {
                loge("map 当前的线程是：${Thread.currentThread().name}")
                it
            }
            .flowOn(Dispatchers.Default)
            .map {
                loge("map2 当前的线程是：${Thread.currentThread().name}")
                it
            }
            .flowOn(Dispatchers.Main)
            .map {
                loge("map3 当前的线程是：${Thread.currentThread().name}")
                it
            }
            .flowOn(Dispatchers.IO)

        viewModelScope.launch(Dispatchers.Main) {
            loge("launch 当前的线程是：${Thread.currentThread().name}")
            f.collect {
                loge("collect 当前的线程是：${Thread.currentThread().name}")
            }
        }
    }

}